﻿using System;
using System.Threading;
using Me.Practice.ElevatorEngine.Client.Reporting;
using DomainEntity = Me.Practice.ElevatorEngine.Domain.Entity;

namespace Me.Practice.ElevatorEngine.Client
{
	class Program
	{
		public static void Main(string[] args)
		{
		    var header = new string[]
		                     {
		                         "/////////////////////////////////////////////////////////////",
		                         "//                                                         //",
		                         "// Elevator Emulator Client                                //",
		                         "//                                                         //",
		                         "// Author: Sergey L. Pravodelov                            //",
		                         "// Email : slpravodelov.job@gmail.com                      //",
		                         "// Cell Phone : +7 (929) 041 0330                          //",
		                         "//                                                         //",
		                         "/////////////////////////////////////////////////////////////"
		                     };

			var client = new ReportServiceContractClient();
			Console.WriteLine("Connecting to WCF service host...");
            Console.Title = "Elevator Simulation (Sketch) Author: Sergey L. Pravodelov";
            Console.WriteLine();

		    do
		    {
                while (!Console.KeyAvailable)
                {
                    DomainEntity.BuildingReport reportBuilding;
                    try
                    { 
                        reportBuilding = client.GetBuildingReport();
                    }
                    catch (Exception e)
                    {
                        Console.Clear();
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(e.Message);
                        Console.ResetColor();
                        Console.WriteLine();
                        Console.WriteLine("Press Escape key to exit");
                        break;
                    }
                   

                    Console.Clear();
                    Console.WriteLine(string.Join("\n", header));
                    Console.WriteLine();

                    foreach (var floorReport in reportBuilding.FloorReports)
                    {
                    	Console.ForegroundColor = (ConsoleColor) floorReport.FloorNumber;
                        Console.WriteLine("Floor: {0} - Persons: {1} ", floorReport.FloorNumber, floorReport.PersonCount);
                        Console.ResetColor();
                    }

                    Console.WriteLine();

                    foreach (var elevatorReport in reportBuilding.ElevatorReports)
                    {
                    	Console.ForegroundColor = (ConsoleColor)elevatorReport.CurrentFloorNumber;
                    	Console.WriteLine("Elevator {0} capacity: {1} Current floor #: {2} Persons in: {3} State: {4}",
                                      elevatorReport.Id,
                                      elevatorReport.CurrentCapacity,
                                      elevatorReport.CurrentFloorNumber,
                                      elevatorReport.CurrentPersonCount,
                                      elevatorReport.State);
                    	Console.ResetColor();
                    }                    
                    
                    Console.WriteLine();
                    Console.WriteLine("Press Escape key to exit");
                    Thread.Sleep(200);
                }

		    } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
		}
	}
}